package com.example.helloword.utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

import android.media.MediaPlayer;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.TableRow.LayoutParams;

import com.example.helloword.GuessActivity;
import com.example.helloword.R;

public abstract class ConfigTile implements Serializable {

	private static final long serialVersionUID = 3128273416743495413L;

	public static final int CANT_ROWS = 4;
	public static final int CANT_TILES_X_ROWS = 5;

	private GuessActivity context;

	protected Map<Integer, Integer> imagesNamesMap;
	protected Map<Integer, Integer> soundsNamesMap;
	protected Map<Integer, Integer> soundsFailedNamesMap;
	
	private List<Integer> indexImagesList = new ArrayList<Integer>();
	private ImageViewTile ivTileActual = null;
	private MediaPlayer mpActual;
	private OnClickListener onClickImageViewListener;

	public abstract void initializeSoundsMap();
	public abstract void initializeSoundsFailedMap();
	public abstract void initializeImagesNamesMap();

	public void initialize(GuessActivity guessActivity) {
		this.context = guessActivity;
		initializeImagesNamesMap();
		initializeSoundsMap();
		initializeSoundsFailedMap();
		randomizeTiles();
	}

	public ImageViewTile getImageView(int index, LayoutParams params1) {
		ImageViewTile imageView = new ImageViewTile(context);
		imageView.setIndex(index);
		imageView.setImageResource(R.drawable.guess);
		imageView.setLayoutParams(params1);
		imageView.setOnClickListener(getOnClickImageViewListener());
		return imageView;
	}

	private OnClickListener getOnClickImageViewListener() {
		if(onClickImageViewListener == null) {
			onClickImageViewListener = new ImageViewTileListener();
		}
		return onClickImageViewListener;
	}

	private boolean hayCoincidencia(int index, int index2) {
		return indexImagesList.get(index) == indexImagesList.get(index2);
	}

	private void randomizeTiles() {
		List<Integer> tmpList = new ArrayList<Integer>();
		for(int i=0; i<CANT_ROWS*CANT_TILES_X_ROWS;i++) {
			tmpList.add(i);
		}
		Collections.shuffle(tmpList);
		for(int i=0; i<CANT_ROWS*CANT_TILES_X_ROWS;i++) {
			indexImagesList.add(tmpList.get(i) % (CANT_ROWS*CANT_TILES_X_ROWS/2));
		}
	}

	private class ImageViewTileListener implements OnClickListener {

    	@Override
    	public void onClick(View v) {
    		ImageViewTile ivt = (ImageViewTile)v;

    		mpActual = null;

    		if(!ivt.isVisible()) {
    			ivt.setVisible(true);
    			if(ivTileActual == null) {
    				ivTileActual = ivt;
    				ivt.setImageResource(imagesNamesMap.get(indexImagesList.get(ivt.getIndex())));
    			} else {
    				ivt.setImageResource(imagesNamesMap.get(indexImagesList.get(ivt.getIndex())));
    				if(hayCoincidencia(ivTileActual.getIndex(), ivt.getIndex())) {
    					ivTileActual = null;
    					mpActual = MediaPlayer.create(context, soundsNamesMap.get(indexImagesList.get(ivt.getIndex()) % 5));
    				} else {
    					mpActual = MediaPlayer.create(context, soundsFailedNamesMap.get(new Random().nextInt(soundsFailedNamesMap.keySet().size())));

    					RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
    					anim.setInterpolator(new LinearInterpolator());
    					anim.setDuration(1000);
    					anim.setAnimationListener(new SinAciertoAnimationListener(ivt));
    					ivt.startAnimation(anim);

    					anim = new RotateAnimation(0f, 350f, 15f, 15f);
    					anim.setInterpolator(new LinearInterpolator());
    					anim.setDuration(1000);
    					anim.setAnimationListener(new SinAciertoAnimationListener(ivTileActual));
    					ivTileActual.startAnimation(anim);
    					ivTileActual = null;
    				}
    				mpActual.start();
    				
    			}
    		}
    	}
		
	}

	private class SinAciertoAnimationListener implements AnimationListener {

		private ImageViewTile iv1;

		public SinAciertoAnimationListener(ImageViewTile iv1) {
			this.iv1 = iv1;
		}

		@Override
		public void onAnimationEnd(Animation arg0) {
			iv1.setImageResource(R.drawable.guess);
			iv1.setVisible(false);
		}

		@Override
		public void onAnimationRepeat(Animation arg0) {
		}

		@Override
		public void onAnimationStart(Animation arg0) {
		}

	}

}